package com.hyc.myvideo.ui.activity.home;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import butterknife.BindView;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.HomeClassAdapter;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.HomeCourse;
import com.hyc.myvideo.ui.base.BaseFActivity;
import com.hyc.utils.StringUtils;
import java.util.ArrayList;
import java.util.List;
import timber.log.Timber;

/**
 * 修炼目录  课程列表
 * Created by dfeads on 2017/8/10.
 */

public class CourseListActivity extends BaseFActivity {

  @BindView(R.id.activity_audio_list_refresh) PullToRefreshListView mRefresh;
  private String catelog_id = "";
  private String catelog_name = "";
  private int page = 1;

  private HomeClassAdapter homeClassAdapter;
  private List<HomeCourse> homeCourseList;

  @Override public int getContentViewId() {
    return R.layout.activity_audio_list;
  }

  @Override protected void initView() {
    catelog_id = getIntent().getStringExtra("id");
    catelog_name = getIntent().getStringExtra("title");
    if (!StringUtils.isEmpty(catelog_name)) {
      setLeftBtnClick(true, catelog_name);
    }

    homeCourseList = new ArrayList<>();
    homeClassAdapter = new HomeClassAdapter(CourseListActivity.this, homeCourseList);
    mRefresh.setAdapter(homeClassAdapter);

    mRefresh.setMode(PullToRefreshBase.Mode.BOTH);
    mRefresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
      @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

      }

      @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

      }
    });
    mRefresh.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        HomeCourse homeCourse = homeCourseList.get(position - 1);
        //1系列 2单个
        if (homeCourse.getState() == 1) {
          startActivity(
              new Intent(CourseListActivity.this, TrainingCatalogActivity.class).putExtra("id",
                  homeCourse.getId()));
        } else {
          startAct(homeCourse);
        }
      }
    });
  }

  /**
   * list的跳转
   */
  private void startAct(HomeCourse hCourse) {
    //1=视频,2=音频,3=图文
    switch (hCourse.getCurriculum_hour_type()) {
      case 1:
        startActivity(new Intent(CourseListActivity.this, VideoActivity.class).putExtra("id",
            hCourse.getId()));
        break;
      case 2:
        startActivity(new Intent(CourseListActivity.this, AudioActivity.class).putExtra("id",
            hCourse.getId()));
        break;
      case 3:
        startActivity(new Intent(CourseListActivity.this, HomeDetailActivity.class).putExtra("id",
            hCourse.getId()));
        break;
    }
  }

  @Override protected void initData() {
    api.getPracticeCatelogCurriculumList(userInfo.getUser_id(), catelog_id, page);
  }

  @Override protected void oNext(String resulte, String method) {
    BaseResult<List<HomeCourse>> homeCourses =
        JSONObject.parseObject(resulte, new TypeReference<BaseResult<List<HomeCourse>>>() {
        });

    homeCourseList.clear();
    homeCourseList.addAll(homeCourses.getResult());
    homeClassAdapter.updateListView(homeCourseList);
  }

  @Override protected void oError() {

  }
}
